// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/InteractionWidget.h"
#include "Components/TextBlock.h"
#include "Libraries/CommonLibrary.h"

#define LOCTEXT_NAMESPACE "BulletScars_Mission"

void UInteractionWidget::NativeConstruct() {
	Super::NativeConstruct();
	UWidgetAnimation* Fade = UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("Fade"));
	PlayAnimation(Fade);
}

void UInteractionWidget::RequestDestruct()
{
	UWidgetAnimation* Fade = UCommonLibrary::GetAnimInstance(GetClass(), FText::FromString("Fade"));
	PlayAnimation(Fade, 0.f, 1.f, EUMGSequencePlayMode::Reverse, 2.f);
	if (GetWorld()->GetTimerManager().IsTimerActive(TimerHandle)) {
		GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
	}
	FTimerDelegate RespawnDelegate = FTimerDelegate::CreateUObject(this, &UInteractionWidget::DelayLater);
	GetWorld()->GetTimerManager().SetTimer(TimerHandle, RespawnDelegate, Fade->GetEndTime() / 2.f, false);
}

void UInteractionWidget::UpdateInteractionText(FKey InKey, FText InAction)
{
	Action->SetText(InAction);
	if (InAction.EqualTo(FText())) {
		Key->SetText(FText::FromString(FString::Printf(TEXT("[]"))));
	}
	else {
		// Key->SetText(FText::FromString(FString::Printf(TEXT("[%s]"), *InKey.GetDisplayName().ToString())));
		Key->SetText(FText::Format(LOCTEXT("Key", "[{0}]"), InKey.GetDisplayName()));
		// FText::Format(LOCTEXT("Max", "{0}+"), FText::AsNumber(MaxDisplayDistance));
	}
}

void UInteractionWidget::DelayLater() {
	RemoveFromParent();
	GetWorld()->GetTimerManager().ClearTimer(TimerHandle);
}

#undef LOCTEXT_NAMESPACE